package com.jinzecloud.reportapproval.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.jinzecloud.reportapproval.bean.ProductVerificaRecord;
import com.jinzecloud.reportapproval.bean.TestsSummaryTable;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.UserSurveySummary;
import com.jinzecloud.reportapproval.service.UserSurveySummaryService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 18:28:40
 */
@RestController
@RequestMapping("reportapproval/usersurveysummary")
public class UserSurveySummaryController {
    @Autowired
    private UserSurveySummaryService userSurveySummaryService;
    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:usersurveysummary:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userSurveySummaryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:usersurveysummary:info")
    public R info(@PathVariable("id") Long id){
		UserSurveySummary userSurveySummary = userSurveySummaryService.getById(id);

        return R.ok().put("userSurveySummary", userSurveySummary);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:usersurveysummary:save")
    public R save(@RequestBody UserSurveySummary userSurveySummary){
        QueryWrapper<UserSurveySummary> temp = new QueryWrapper<UserSurveySummary>().eq("project_num", userSurveySummary.getProjectNum());
        List<UserSurveySummary> list = userSurveySummaryService.list(temp);
        if(list.size()>0){
            return R.error("此编号已存在,请勿重复添加");
        }

        userSurveySummary.setAuditStatus(String.valueOf(StateEnum.UN_SUBMIT.getState())).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        userSurveySummary.setCreatorId(String.valueOf(adminService.getIdByName(authentication.getName())));

		userSurveySummaryService.save(userSurveySummary);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:usersurveysummary:update")
    public R update(@RequestBody UserSurveySummary userSurveySummary){
		userSurveySummaryService.updateById(userSurveySummary);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:usersurveysummary:delete")
    public R delete(@RequestBody Long[] ids){
		userSurveySummaryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 生成word
     */
    @RequestMapping("/createPdf/{id}")
    public String createWord(@PathVariable Integer id){
        return userSurveySummaryService.createPdf(id);
    }

    @PostMapping("/review")
    public R review(@RequestBody UserSurveySummary userSurveySummary) {
        userSurveySummaryService.updateById(new UserSurveySummary().setId(userSurveySummary.getId()).setAuditStatus(String.valueOf(0)));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return userSurveySummaryService.audit(auditParam);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(userSurveySummaryService.auditList(param));
    }
}
